package cn.codeforfun.follower;

import cn.codeforfun.common.WechatService;
import cn.codeforfun.utils.HttpKit;
import com.fasterxml.jackson.databind.ObjectMapper;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.transaction.Transactional;
import java.io.IOException;
import java.util.List;

/**
 * Created by Administrator on 2017/2/16 0016.
 */
@Service
@Transactional
public class FollowerService {
    @Autowired
    private FollowerDao followerDao;
    @Autowired
    private WechatService wechatService;

    public Follower findOne(Long followerId) {
        return followerDao.findOne(followerId);
    }

    public List<Follower> findAll() {
        return followerDao.findAll();
    }

    public Follower save(Follower follower) {
        followerDao.save(follower);
        return follower;
    }


    public Follower sysncFollower(String openId) throws WxErrorException {
        if (StringUtils.isEmpty(openId)) return null;
        List<Follower> byOpenId = followerDao.findByOpenId(openId);
        if (byOpenId != null) return byOpenId.get(0);
        WxMpUser wxMpUser = wechatService.getService().getUserService().userInfo(openId);
        Follower follower = getByOpenId(openId, wxMpUser);
        return follower;
    }

    public Follower getByOpenId(String openId, WxMpUser wxMpUser) {
        List<Follower> byOpenId = followerDao.findByOpenId(openId);
        Follower follower;
        if (!CollectionUtils.isEmpty(byOpenId)) {
            follower = byOpenId.get(0);
            follower.setSubscribe(wxMpUser.getSubscribe());
            follower.setNickname(wxMpUser.getNickname());
            follower.setSex(wxMpUser.getSex());
            follower.setLanguage(wxMpUser.getLanguage());
            follower.setCity(wxMpUser.getCity());
            follower.setProvince(wxMpUser.getProvince());
            follower.setCountry(wxMpUser.getCountry());
            follower.setHeadImgUrl(wxMpUser.getHeadImgUrl());
            follower.setSubscribeTime(wxMpUser.getSubscribeTime());
            follower.setSexId(wxMpUser.getSexId());
            follower.setRemark(wxMpUser.getRemark());
        } else {
            follower = new Follower(wxMpUser);
        }
        followerDao.save(follower);
        return follower;
    }

    public Follower snsFollower(String code) throws WxErrorException, IOException {
        WxMpOAuth2AccessToken wxMpOAuth2AccessToken = wechatService.getService().oauth2getAccessToken(code);
        WxMpOAuth2AccessToken wxMpOAuth2AccessToken1 = wechatService.getService().oauth2refreshAccessToken(wxMpOAuth2AccessToken.getRefreshToken());
        WxMpUser wxMpUser = wechatService.getService().oauth2getUserInfo(wxMpOAuth2AccessToken1, null);
        Follower follower = getByOpenId(wxMpUser.getOpenId(), wxMpUser);
        return follower;
    }
}
